<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
</body>
<script>
    // Symbol应用:
    // (1) 最为对象的键名

    // var random = Symbol("random");

    // Math[random] = function (min = 0, max) {
    //     return min + Math.round(Math.random() * (max - min))
    // }
    // console.log(Math);

    // var result = Math[random](0, 100);
    // console.log(result);

    // (2) 消除魔术字符串
    // 魔术字符串指的是，在代码之中多次出现、与代码形成强耦合的某一个具体的字符串或者数值。风格良好的代码，应该尽量消除魔术字符串，改由含义清晰的变量代替。

    // function validate(type, val) {
    //     // type = "user";
    //     var rules = {
    //         user: /^[a-zA-Z_$][\w$]{5,11}$/,
    //         pwd: /^[\w$]{6,12}$/,
    //         phone: /^1[3-9]\d{9}$/,
    //         email: /^\w{6,12}@\w+\.(com|cn|email)$/,
    //     }

    //     if (type == "user") {
    //         return rules.user.test(val);
    //     } else if (type == "pwd") {
    //         return rules.pwd.test(val);
    //     } else if (type == "phone") {
    //         return rules.phone.test(val);
    //     } else if (type == "email") {
    //         return rules.email.test(val);
    //     } else {
    //         return false;
    //     }
    // }

    // validate("user", "a123123");

    // 消除魔术字符串
    // var types = {
    //     user: "user",
    //     pwd: "pwd",
    //     phone: "phone",
    //     email: "email",
    // }
    var types = {
        user: Symbol(),
        pwd: Symbol(),
        phone: Symbol(),
        email: Symbol(),
    }

    function validate(type, val) {
        // type = "user";
        var rules = {
            user: /^[a-zA-Z_$][\w$]{5,11}$/,
            pwd: /^[\w$]{6,12}$/,
            phone: /^1[3-9]\d{9}$/,
            email: /^\w{6,12}@\w+\.(com|cn|email)$/,
        }

        if (type == types.user) {
            return rules.user.test(val);
        } else if (type == types.pwd) {
            return rules.pwd.test(val);
        } else if (type == types.phone) {
            return rules.phone.test(val);
        } else if (type == types.email) {
            return rules.email.test(val);
        } else {
            return false;
        }
    }

    validate(types.user, "a123123");



</script>

</html>